<?php
//INS#KRT#320101000100101#jl layang#RW 02#RT 02#2#4000#3000#1046#DUSUN 01
$handle = @fopen("smsdrc1", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle);

		if (substr_count($buffer, 'user = ') > 0)
		{
		   $split = explode("user = ", $buffer);
		   $user = str_replace("\r\n", "", $split[1]);
		}

		if (substr_count($buffer, 'password = ') > 0)
		{
		   $split = explode("password = ", $buffer);
		   $pass = str_replace("\r\n", "", $split[1]);
		}

		if (substr_count($buffer, 'database = ') > 0)
		{
		   $split = explode("database = ", $buffer);
		   $db = str_replace("\n", "", $split[1]);
		}
		
    }
    fclose($handle);
}

mysql_connect('localhost', $user, $pass);
mysql_select_db($db);

$query = "SELECT * FROM inbox ORDER BY ID DESC";
$hasil = mysql_query($query) or die(mysql_error());
echo "<table border='1'>";
echo "<tr><th>ReceivingDateTime</th><th>SenderNumber</th><th>TextDecoded</th><th>Phone ID</th></tr>";
while ($data = mysql_fetch_array($hasil))
{
   echo "<tr><td>".$data['ReceivingDateTime']."</td><td>".$data['SenderNumber']."</td><td>".$data['TextDecoded']."</td><td>".$data['RecipientID']."</td></tr>";
   
   
   /*
   $sql = "INSERT INTO `krt` (`id_daerah`, `dusun`) VALUES ('$word[2]','$word[3]')";
   if (mysql_query($sql,$con1)){
  echo($LF."save record success...");
  }*/
}

$q1 = "SELECT * FROM art ORDER BY id DESC LIMIT 1";
$h1 = mysql_query($q1) or die(mysql_error());
$last = "";
while ($d1 = mysql_fetch_array($h1))
{
	$last = $d1['lastid'];
}

$qidkrt = "SELECT * FROM art ORDER BY id DESC LIMIT 1";
$hidkrt = mysql_query($qidkrt) or die(mysql_error());
$last_idkrt = "";
while ($didkrt = mysql_fetch_array($hidkrt))
{
	$last_idkrt = $didkrt['id_krt'];
	$last_idkrt = $last_idkrt + 1;
}

$query1 = "SELECT * FROM inbox WHERE dikirim = 0 ORDER BY ID DESC LIMIT 1";
$hasil1 = mysql_query($query1) or die(mysql_error());
$no = 0;
$isi = "";
$idinbox = "";
while ($data1 = mysql_fetch_array($hasil1))
{
	$no++;
	/*
	$idinbox = $data1['ID'];
	if($data1['ID'] != $last){
		$isi = $data1['TextDecoded'];
	}*/	
	
	$isi = $data1['TextDecoded'];
}
	

//echo "no : $no";  INS#KRT#1046#DUSUN 01
//id#no_kip#id_daerah#id_klasifikasi

//INS#KRT#no_kip#id_daerah#id_klasifikasi#NAMA#ALAMAT#RT#RW#JMLART#X#Y#KD#DUSUN#KP#KK#PIN
//INS#KRT#KIP#NAMA#ALAMAT#RT#RW#JMLART#X#Y#KD#DUSUN#KP#KK#PIN";


//INS#KRT#no_kip#alamat#rt#rw#jml_keluarga#x#y#id_daerah#dusun;
//INS#KRT#320101000100101#jl layang#RW 02#RT 02#2#4000#3000#1046#DUSUN 01;


//UPD#KRT#no_kip#alamat#rt#rw#jml_keluarga#x#y#id_daerah#dusun;
//UPD#KRT#no_kip#alamat#rt#rw#jml_keluarga#x#y#id_daerah#dusun;
//UPD#KRT#320101000100101#jl layang#RW 02#RT 02#2#4000#3000#1046#DUSUN 01;


echo "</table>";
$ada = "";
$LF = "<br>";
if($isi != ""){
	//$echo
	$word = explode("#", $isi);
	$nodata = 0;
	foreach($word as &$data){
		//echo($LF.$data);
		$nodata++;
	}	
	//echo 'coy'.$word[0] .  "<br>";
	if($nodata == 12){
		if($word[0] == "INS"){		
		//INS#KRT#no_kip#nama#alamat#rt#rw#jml_keluarga#x#y#id_daerah#dusun;
		//INS#KRT#320101000100101#arief#haruman#RT 07#RW 06#1#430#550#1046#DUSUN 01;
		/*
		$word[0]INS#
		$word[1]KRT#
		$word[2]no_kip#
		$word[3]nama#
		$word[4]alamat#
		$word[5]rt#
		$word[6]rw#
		$word[7]jml_keluarga#
		$word[8]x#
		$word[9]y#
		$word[10]id_daerah#
		$word[11]dusun;
		*/
		
		$sql = "INSERT INTO `krt` (`no_kip`, `alamat`, `rt`, `rw`, `jml_keluarga`, `x`, `y`, `id_daerah`, `dusun`) VALUES ('$word[2]','$word[4]','$word[5]','$word[6]','$word[7]','$word[8]','$word[9]','$word[10]','$word[11]')";
		
		$result = mysql_query($sql);
		
			$sql = "INSERT INTO `art` (`nama_art`,`id_hubkrt`,`id_krt`) VALUES ('$word[3]','1','$last_idkrt')";
			mysql_query($sql)
			or die(mysql_error());  		
			
			$sql = "UPDATE `inbox` SET `dikirim` = 1 WHERE `dikirim` = 0 ";
			mysql_query($sql)
			or die(mysql_error()); 
			
				
		}else if($word[0] == "UPD"){
		
				
		$sql = "UPDATE `krt` SET alamat = '$word[4]', rt = '$word[5]', rw = '$word[6]', jml_keluarga = '$word[7]', x = '$word[8]', y = '$word[9]', id_daerah = '$word[10]', dusun = '$word[11]' WHERE no_kip='$word[2]'";
		mysql_query($sql)
		or die(mysql_error());  		
		
		$id_krt = substr($word[2],12,3);
		$sql = "UPDATE `art` SET nama_art = '$word[3]' WHERE id_krt='$id_krt' AND id_hubkrt=1";
		mysql_query($sql)
		or die(mysql_error());  
		
		$sql = "UPDATE `inbox` SET `dikirim` = 1 WHERE `dikirim` = 0 ";
		mysql_query($sql)
		or die(mysql_error());  
		}
	}
	
}
?>